package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import sun.security.util.DerValue;
import sun.security.x509.AVA;
import sun.security.x509.X500Name;

import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 01.12.2011
 * Time: 11:09
 * To change this template use File | Settings | File Templates.
 */
public class EName {
    public EName(X500Name name) {
        this.name = name;
    }

    X500Name name;

    public String getString()
    {
        if(name == null)
        {
            return "";
        }
        String retStr =  name.toString();
        retStr = retStr.replaceAll(", ","\n");
        return retStr;
    }

    public String toTitle()
    {
        if(name == null)
        {
            return "";
        }
        String retStr="";
        String cn = null;
        try {
            cn = name.getCommonName();
             if(cn != null)
        {
            retStr = cn;
            String sn =name.getSurname();
            if(sn != null)
            {
                retStr+=" "+sn;
            }
        }
        else
        {
            String ou = name.getOrganizationalUnit();
            if(ou != null)
            {
                retStr = ou;
            }
            else
            {
                String o = name.getOrganization();
                if(o!=null)
                {
                    retStr = o;
                }
                /*
                else
                {
                    String serialNumber = name.getser.getSerialNumberAttribute();
                    if(serialNumber != null)
                    {
                        retStr = serialNumber;
                    }
                } */
            }
        }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return retStr;
    }
}
